package com.paomi.client.activity.message;

import android.app.Activity;
import android.app.Dialog;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.paomi.client.R;
import com.paomi.client.activity.detail.model.Comment;
import com.paomi.client.activity.message.model.SystemMsgJson;
import com.paomi.client.base.BaseLoadMorePresenter;
import com.paomi.client.base.BaseMvpLCEView;
import com.paomi.client.bean.BaseJSON;
import com.paomi.client.fragment.massage.model.UnMyMsgBean;
import com.paomi.client.net.Api;
import com.paomi.client.net.BaseCallback;
import com.paomi.client.net.Constants;
import com.paomi.client.utils.ToastUtil;
import com.paomi.client.view.LoadingDialog;
import com.paomi.client.widget.RxToast;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

/**
 * Created by pc on 2018/11/9
 * Describe:
 * com.paomi.client.activity.reply
 */
public class MessageListPresenter extends BaseLoadMorePresenter<BaseMvpLCEView> {
    private List<SystemMsgJson.SystemMsgBean> commentList = new ArrayList<>();

    @Override
    protected void loadData(int page, int skip, boolean pullToRefresh) {
        Map<String, String> maps = new HashMap<>();
        maps.put("pageNum", "" + page);
        maps.put("pageSize", "" + Constants.PAGE_SIZE);
        Api.getInstanceService(Constants.Ticket_URL).mySystemList(maps).enqueue(new BaseCallback<SystemMsgJson>() {
            @Override
            public void onResponse(Call<SystemMsgJson> call, SystemMsgJson result) {
                if (page == 1) {
                    commentList.clear();
                }
                if (result.getReturnData().getData() != null) {
                    commentList.addAll(result.getReturnData().getData());
                    getView().setData(commentList, false);
                }

            }
        });
        if (commentList.size() == 0) {
            if(isViewAttached())
            getView().setData(commentList, false);
        }

    }

    /**
     * 点击已读
     *
     * @param id
     */
    public void clickReaded(Activity activity, String id) {
        LayoutInflater inflater = LayoutInflater.from(activity);
        View view = inflater.inflate(R.layout.dialog_delete_shaow, null);
        final Dialog dialog = new Dialog(activity, R.style.theme_dialog);
        dialog.setContentView(view);
        dialog.show();
        TextView title = view.findViewById(R.id.title);
        title.setText(activity.getResources().getString(R.string.edit_read));
        TextView msg_tv = view.findViewById(R.id.msg_tv);
        msg_tv.setText(activity.getResources().getString(R.string.edit_isread));
        TextView textCancel = view.findViewById(R.id.btn_qd);
        textCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        TextView textSure = view.findViewById(R.id.btn_success);
        textSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Api.getInstanceService(Constants.Ticket_URL).readNotifyList(id).enqueue(new BaseCallback<BaseJSON>() {
                    @Override
                    public void onResponse(Call<BaseJSON> call, BaseJSON result) {
                        loadData(true);
                        //全部已读的操作
                        BaseJSON baseJSON = new BaseJSON();
                        baseJSON.returnMessage = "已读成功";
                        new Handler().postDelayed(new Runnable() {
                            public void run() {
                                EventBus.getDefault().post(baseJSON);
                            }
                        }, 100);
                    }
                });
            }
        });
    }
    /**
     * 点击全部已读
     *
     * @param
     */
    public void clickReadedAll(Activity activity) {
        LayoutInflater inflater = LayoutInflater.from(activity);
        View view = inflater.inflate(R.layout.dialog_delete_shaow, null);
        final Dialog dialog = new Dialog(activity, R.style.theme_dialog);
        dialog.setContentView(view);
        dialog.show();
        TextView title = view.findViewById(R.id.title);
        title.setText(activity.getResources().getString(R.string.edit_read));
        TextView msg_tv = view.findViewById(R.id.msg_tv);
        msg_tv.setText(activity.getResources().getString(R.string.edit_isread_all));
        TextView textCancel = view.findViewById(R.id.btn_qd);
        textCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        TextView textSure = view.findViewById(R.id.btn_success);
        textSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                LoadingDialog.get().showLoading();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        String id="";
                        for (int i=0;i<commentList.size();i++){
                            SystemMsgJson.SystemMsgBean systemMsgBean=commentList.get(i);
                            id +=""+systemMsgBean.getId()+",";
                        }
                        id=id.substring(0,id.length()-1);
                        Api.getInstanceService(Constants.Ticket_URL).readNotifyList(id).enqueue(new BaseCallback<BaseJSON>() {
                            @Override
                            public void onResponse(Call<BaseJSON> call, BaseJSON result) {
                                LoadingDialog.get().hideLoading();
//                        ToastUtil.getToastEmail().ToastShow(activity, null, 1, R.layout.toast_view, "标记成功");
                                loadData(true);
                                //全部已读的操作
                                BaseJSON baseJSON = new BaseJSON();
                                baseJSON.returnMessage = "全部已读";
                                new Handler().postDelayed(new Runnable() {
                                    public void run() {
                                        EventBus.getDefault().post(baseJSON);
                                    }
                                }, 100);
                            }
                        });
                    }
                },100);
            }
        });


//        LayoutInflater inflater = LayoutInflater.from(activity);
//        View view = inflater.inflate(R.layout.dialog_delete_shaow, null);
//        final Dialog dialog = new Dialog(activity, R.style.theme_dialog);
//        dialog.setContentView(view);
//        dialog.show();
//        TextView title = view.findViewById(R.id.title);
//        title.setText(activity.getResources().getString(R.string.edit_read));
//        TextView msg_tv = view.findViewById(R.id.msg_tv);
//        msg_tv.setText(activity.getResources().getString(R.string.edit_isread_all));
//        TextView textCancel = view.findViewById(R.id.btn_qd);
//        textCancel.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                dialog.dismiss();
//            }
//        });
//        TextView textSure = view.findViewById(R.id.btn_success);
//        textSure.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                dialog.dismiss();
//
//            }
//        });
    }
    /**
     * 点击删除
     *
     * @param id
     */
    public void deleteReaded(Activity activity,String id) {
        LayoutInflater inflater = LayoutInflater.from(activity);
        View view = inflater.inflate(R.layout.dialog_delete_shaow, null);
        final Dialog dialog = new Dialog(activity, R.style.theme_dialog);
        dialog.setContentView(view);
        dialog.show();
        TextView title = view.findViewById(R.id.title);
        title.setText(activity.getResources().getString(R.string.comment_delete));
        TextView msg_tv = view.findViewById(R.id.msg_tv);
        msg_tv.setText(activity.getResources().getString(R.string.edit_isread_delete));
        TextView textCancel = view.findViewById(R.id.btn_qd);
        textCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        TextView textSure = view.findViewById(R.id.btn_success);
        textSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Api.getInstanceService(Constants.Ticket_URL).readNotifyDelete(id).enqueue(new BaseCallback<BaseJSON>() {
                    @Override
                    public void onResponse(Call<BaseJSON> call, BaseJSON result) {
                        loadData(true);
                        //删除成功的操作
                        BaseJSON baseJSON = new BaseJSON();
                        baseJSON.returnMessage = "删除成功";
                        new Handler().postDelayed(new Runnable() {
                            public void run() {
                                EventBus.getDefault().post(baseJSON);
                            }
                        }, 100);
                    }
                });
            }
        });

    }

    public void getNum() {
        Api.getInstanceService(Constants.Ticket_URL).myUnMsgAll().enqueue(new BaseCallback<UnMyMsgBean>() {
            @Override
            public void onResponse(Call<UnMyMsgBean> call, UnMyMsgBean result) {
                //举报成功的操作
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        EventBus.getDefault().post(result);
                    }
                }, 100);
            }
        });
    }
}
